<?php
namespace Announce\Controller;
use Think\Controller;

/**
 * Class AdminController
 * @package Announce\Controller
 * 公告后台
 */
class AdminController extends Controller {

    private $model_name = 'Announce';
    public function index(){
        $page_list = get_page_list($this->model_name);
        $this->assign('list', $page_list['list']);
        $this->assign('page', $page_list['show']);
        $this->display();
    }

    public function add(){
        if(IS_POST){
            $post_data = filterInput($_POST);
            $model = D('Announce');
            $model->addObj($post_data);
            $this->success(LANG('save_success'));
            return;
        }
        $this->display();
    }

    public function edit(){
        if(IS_POST){
            $post_data = filterInput($_POST);
            $model = D('Announce');
            $model->save($post_data);
            $this->success(LANG('save_success'));
            return;
        }else if(IS_GET){
            $data = filterInput($_GET);
            $model = D('Announce');
            $item = $model->queryObj($data['id'], 'id');
            $this->assign('item', $item);
        }
        $this->display();
    }

    public function del(){
        $data = filterInput($_POST);
        $model = D('Announce');
        $ids = explode(',', $data['ids']);
        $ids = array_filter($ids);
        foreach($ids as $id){
            $model->delete($id);
        }
    }

    public function cancel(){
        $data = filterInput($_POST);
        $model = D('Announce');
        $ids = explode(',', $data['ids']);
        $ids = array_filter($ids);
        foreach($ids as $id){
            $one = $model->queryObj($id, 'id');
            $one['status'] = 0;
            $model->save($one);
        }
    }
}